package asapIs134CK.womi.client.profile;

import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.FroggitEditorWidget;
import asapIs134CK.womi.client.profile.admintools.UserManagmentToolWidget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class AuthenticatedUserDisplay extends Composite{
	private UserDisplay _parent;
	private UserClient _user;
	
	
	private VerticalPanel panel;
	private VerticalPanel scores;
	private Label nickName;
	private Label score;
	private Label scoreFrog;
	private Label scoreSud;
	private Label scoreAri;
	private FlowPanel actions;
	private Button edit;
	private ClickHandler editClickhandler;
	private Button admin;
	private ClickHandler adminClickHandler;
	private Button tester;
	private ClickHandler testClickHandler;
	
	public AuthenticatedUserDisplay(UserClient user, UserDisplay parent){
		_parent = parent;
		_user = user;
		panel = new VerticalPanel();
		initWidget(panel);
		initContent();
	}
	
	private void initContent(){
		nickName = new Label(_user.getNickname());
		nickName.setStyleName("NickName");
		panel.add(nickName);
		scores = new VerticalPanel();
		scores.addStyleName("Scores");
		score = new Label("Total score : "+_user.getScore());
		scores.add(score);
		scoreFrog = new Label("Froggit score : "+_user.getScoreFroggit());
		scores.add(scoreFrog);
		scoreSud = new Label("Sudoku score : "+_user.getScoreSudoku());
		scores.add(scoreSud);
		scoreAri = new Label("Arithmetic quizz score : "+_user.getScoreArithmeticQuizz());
		scores.add(scoreAri);
		panel.add(scores);
		
		actions = new FlowPanel();
		editClickhandler = new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				editRequested();
			}
			
		};
		edit = new Button("Edit", editClickhandler);
		actions.add(edit);
		if(_user.isAdmin()){
			adminClickHandler = new ClickHandler(){

				@Override
				public void onClick(ClickEvent event) {
					adminRequested();
				}
				
			};
			admin = new Button("Admin", adminClickHandler);
			actions.add(admin);
		}
		if(_user.isTestCreator()){
			testClickHandler = new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					testRequested();
				}

				
			};
			tester = new Button("Test", testClickHandler);
			actions.add(tester);
		}
		panel.add(actions);
		
	}

	private void testRequested() {
		final DialogBox testbox = new DialogBox();
		Button back = new Button("Back", new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				testbox.hide();
			}
		});
		VerticalPanel testPanel = new VerticalPanel();
		testPanel.add(new FroggitEditorWidget());
		testPanel.add(back);
		testbox.add(testPanel);
		testbox.setAnimationEnabled(true);
		testbox.showRelativeTo(RootPanel.get("menu"));
	}
	
	private void editRequested() {
		Window.alert("NOT IMPLEMENTED");
	}

	private void adminRequested() {
		DialogBox admin = new DialogBox();
		UserManagmentToolWidget man = new UserManagmentToolWidget();
		admin.add(man );
		admin.center();
		admin.addAutoHidePartner(man.getElement());
		admin.setAutoHideEnabled(true);
		CloseHandler<PopupPanel> closeHandler = new CloseHandler<PopupPanel>(){

			@Override
			public void onClose(CloseEvent<PopupPanel> event) {
				UserDisplay.getInstance().refresh();
			}};
		admin.addCloseHandler(closeHandler );
		admin.setTitle("Administration");
		admin.setAnimationEnabled(true);
		admin.showRelativeTo(RootPanel.get("menu"));
	}
}
